/**   
 * @Title: MyException.java
 * @Package com.nbst.comnutil
 * @Description:
 * @author King  
 * @date 2018-9-13  
 */

package com.nbst.comnutil;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: MyException
 * @Description:自定义异常
 * @author King
 * @date 2018-9-13
 * 
 */

public class MyException extends Exception {

	private static final long serialVersionUID = 8556589528714178015L;

	protected String expCode;
	protected String expMsg;
	protected String childMsg;
	protected List<MyException> childExpList;

	// 用于说明异常所发生时间和位置的属性
	protected String respTimestamp;
	protected String hostName;
	protected String ipAddress;
	// 针对胖Java客户端情况，客户端需要记录异常的栈信息以便于前后端联调。
	protected String expStackTrace;

	private void initAttr(String expCode) {
		this.expCode = expCode;
		this.expMsg = this.getMessage();
		this.childMsg = "";
		childExpList = new ArrayList<MyException>();
	}

	public MyException() {
		childExpList = new ArrayList<MyException>();
	}

	/**
	 * 在提供了消息代码的情况下构建对象。
	 * 
	 * @param expCode
	 * @param msgParam
	 */
	public MyException(String expCode) {
		super(ResourceReader.getMsg(expCode));
		initAttr(expCode);
	}


	/**
	 * 在提供了消息代码和消息参数的情况下构建对象。
	 * 
	 * @param expCode
	 * @param msgParam
	 */
	public MyException(String expCode, String[] msgParam) {
		super(ResourceReader.getMsg(expCode, msgParam));
		initAttr(expCode);
	}

	/**
	 * 在提供了消息代码和根异常的情况下构建对象。
	 * 
	 * @param expCode
	 * @param msgParam
	 */
	public MyException(String expCode, Throwable cause) {
		super(ResourceReader.getMsg(expCode), cause);
		initAttr(expCode);
	}

	/**
	 * 在提供了消息代码、消息参数和根异常的情况下构建对象。
	 * 
	 * @param expCode
	 * @param msgParam
	 */
	public MyException(String expCode, String[] msgParam, Throwable cause) {
		super(ResourceReader.getMsg(expCode, msgParam), cause);
		initAttr(expCode);
	}

	/**
	 * 追加所包含的子异常。
	 * 
	 * @param expCode
	 * @param msgParam
	 */
	public void addChildExp(MyException appExp) {

		if (childExpList == null) {
			childExpList = new ArrayList<MyException>();
		}

		childExpList.add(appExp);

		// 将给定异常的消息追加到已有消息中。
		if (childExpList.size() == 1) {
			// expMsg = expMsg + ": " + appExp.getExpMsg();
			childMsg = appExp.getExpMsg();
		} else {
			// expMsg = expMsg + "; " + appExp.getExpMsg();
			childMsg = appExp.getExpMsg() + " <- " + childMsg;
		}

	}

	private String code;  //异常状态码

	private String message;  //异常信息

	public MyException(String code, String message) {
		this.code=code;
		this.message=message;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getExpCode() {
		return expCode;
	}

	public String getExpMsg() {
		return expMsg;
	}

	public String getChildMsg() {
		return childMsg;
	}

	public void setChildMsg(String childMsg) {
		this.childMsg = childMsg;
	}

	public List<MyException> getChildExpList() {
		return childExpList;
	}

	public String getRespTimestamp() {
		return respTimestamp;
	}

	public void setRespTimestamp(String respTimestamp) {
		this.respTimestamp = respTimestamp;
	}

	public String getHostName() {
		return hostName;
	}

	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	public String getExpStackTrace() {
		return expStackTrace;
	}

	public void setExpStackTrace(String expStackTrace) {
		this.expStackTrace = expStackTrace;
	}

}
